<template>
	<view class="content">
		<!-- 轮播图 -->
		<IndexSwiper :lbData="lbData"></IndexSwiper>
		<!-- 导航 -->
		<IndexNav></IndexNav>
		<!-- 卡片 -->
		<IndexCard></IndexCard>
		<view class="goods-list">
			<GoodsItem v-for="item in goodsList" :key="item.id" :item="item"></GoodsItem>
		</view>
		<view v-if="btmFlag" class="btm">
			------------触底呐！-----------
		</view>
	</view>
</template>

<script>
	import IndexSwiper from '../../components/index/Index-swiper.vue'
	import IndexNav from '../../components/index/Index-nav.vue'
	import IndexCard from '../../components/index/Index-card.vue'
	import GoodsItem from '../../components/common/GoodsItem.vue'
	export default {
		components: {
			IndexSwiper,
			IndexNav,
			IndexCard,
			GoodsItem
		},
		data() {
			return {
				lbData: [],
				goodsList: [],
				pageindex: 1,
				btmFlag: false
			}
		},
		onLoad() {
			this.getLbData()
			this.getGoodsList()
		},
		methods: {
			async getLbData() {
				this.lbData = await this.$myRequest({
					url: '/api/getlunbo'
				})

			},
			async getGoodsList(callback) {
				const res = await this.$myRequest({
					url: '/api/getgoods?pageindex=' + this.pageindex
				})
				callback && callback()
				this.goodsList = [...this.goodsList, ...res]
			}
		},
		// 触底加载
		onReachBottom() {
			if (this.goodsList.length < (this.pageindex * 10)) return this.btmFlag = true
			this.pageindex++
			this.getGoodsList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.goodsList = []
			this.pageindex = 1
			this.getGoodsList(() => {
				setTimeout(() => {
					uni.stopPullDownRefresh()
				}, 1000)

			})
		}

	}
</script>

<style lang="scss" scoped>
	.content {
		width: 750rpx;

		.goods-list {
			width: 100%;
			padding: 14rpx 12rpx 0rpx 12rpx;
			box-sizing: border-box;
			background-color: #eeeeee;
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
		}

		.btm {
			width: 100%;
			background-color: #eeeeee;
			text-align: center;
			font-size: 26rpx;
			color: #c41d01;

		}
	}
</style>